<?php
namespace Qy {
    /**
    * 企业微信素材文件管理类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E7%B4%A0%E6%9D%90%E6%96%87%E4%BB%B6
    */
    class Media {
        private $common;
        private $media_type = array('image', 'voice', 'video', 'file',);
        
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 上传临时素材文件 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E4%B8%B4%E6%97%B6%E7%B4%A0%E6%9D%90%E6%96%87%E4%BB%B6
        * 
        * @param mixed $path
        * @param mixed $type
        * @param AccessToken $a_token
        */
        public function uploadTempMedia($path, $type='file', $a_token = '') {
            if (empty($path)) return false;
            if (!file_exists($path)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            if (!in_array($type,$this->media_type)) $type = 'file';
            if(class_exists('CURLFile')) {
                $data = array(
                    'media' => new CURLFile(ZW_ROOT . '/' . ltrim($path, '/'))
                );
            } else {
                $data = array(
                    'media' => '@' . ZW_ROOT . '/' . ltrim($path, '/')
                );
            }
            
            $url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={$a_token}&type={$type}";
            return $this->common->uploadFileByPost($url,$data);
        }
        
        /**
        * 下载临时素材文件，需要按HTTP头处理
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E4%B8%B4%E6%97%B6%E7%B4%A0%E6%9D%90%E6%96%87%E4%BB%B6
        * 
        * @param mixed $media_id
        * @param AccessToken $a_token
        * @return mixed
        */
        public function getTempMedia($media_id,$a_token = '') {
            if (empty($media_id)) return '';
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token={$a_token}&media_id={$media_id}";
            $ret = $this->common->httpRequest($url);
            if ($ret) {
                return $ret;
            }else{
                return array('url' => $url);
            }
        }
        
        /**
        * 上传永久图文素材
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function addMpnews($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['mpnews']) || !is_array($ary['mpnews'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/material/add_mpnews?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 上传其他类型永久素材
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
        * 
        * @param mixed $path
        * @param mixed $type
        * @param AccessToken $a_token
        */
        public function addMaterial($path, $type='file', $a_token = '') {
            if (empty($path)) return false;
            if (!file_exists($path)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            if (!in_array($type,$this->media_type)) $type = 'file';
            if(class_exists('CURLFile')) {
                $data = array(
                    'media' => new CURLFile(ZW_ROOT . '/' . ltrim($path, '/'))
                );
            } else {
                $data = array(
                    'media' => '@' . ZW_ROOT . '/' . ltrim($path, '/')
                );
            }
            
            $url = "https://qyapi.weixin.qq.com/cgi-bin/material/add_material?type={$type}&access_token={$a_token}";
            return $this->common->uploadFileByPost($url,$data);
        }
        
        /**
        * 获取永久素材 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
        * 
        * @param mixed $media_id
        * @param AccessToken $a_token
        * @return mixed
        */
        public function getMaterial($media_id, $a_token = '') {
            if (empty($media_id)) return '';
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/material/get?access_token={$a_token}&media_id={$media_id}";
            $ret = $this->common->httpRequest($url);
            if ($ret) {
                return $ret;
            }else{
                return array('url' => $url);
            }
        }
        
        /**
        * 删除永久素材 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%A0%E9%99%A4%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
        * 
        * @param mixed $media_id
        * @param AccessToken $a_token
        * @return mixed
        */
        public function delMaterial($media_id, $a_token = '') {
            if (empty($media_id)) return '';
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/material/del?access_token={$a_token}&media_id={$media_id}";
            return $this->common->httpRequest($url);
        }
        
        /**
        * 修改永久图文素材 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%BF%AE%E6%94%B9%E6%B0%B8%E4%B9%85%E5%9B%BE%E6%96%87%E7%B4%A0%E6%9D%90
        * 
        * @param mixed $ary
        * @param AccessToken $a_token
        */
        public function updateMpnews($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['media_id'])) return false;
            if (!isset($ary['mpnews']) || !is_array($ary['mpnews'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/material/update_mpnews?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 获取素材总数 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E6%80%BB%E6%95%B0
        * 
        * @param AccessToken $a_token
        */
        public function getCountMaterial($a_token = '') {
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/material/get_count?access_token={$a_token}";
            return $this->common->httpRequest($url);
        }
        
        /**
        * 获取素材列表 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E5%88%97%E8%A1%A8
        * 
        * @param mixed $type
        * @param mixed $offset
        * @param mixed $count
        * @param mixed $a_token
        */
        public function batchgetMaterial($type = 'file', $offset = 0, $count = 10, $a_token = '') {
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            if (!in_array($type,$this->media_type)) $type = 'file';
            $url = "https://qyapi.weixin.qq.com/cgi-bin/material/batchget?access_token={$a_token}";
            $ary = array('type' => $type, 'offset' => $offset, 'count' => $count);
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 上传图文消息内的图片 
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E5%9B%BE%E6%96%87%E6%B6%88%E6%81%AF%E5%86%85%E7%9A%84%E5%9B%BE%E7%89%87
        * 
        * @param mixed $path
        * @param AccessToken $a_token
        */
        public function uploadimgMedia($path, $a_token = '') {
            if (empty($path)) return false;
            if (!file_exists($path)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $data = array('media' => '@' . ZW_ROOT . '/' . ltrim($path, '/'));
            $url = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$a_token}";
            return $this->common->uploadFileByPost($url,$data);
        }
    }
}
?>
